﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using KPIS.DBM;
using KPIS.GERP.GHRMS.IDAL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class MasReligionDAL : IMasReligionDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public MasReligionDAL()
        { 
        
        }
        public IList<MasReligionInfo> ListReligions(MasReligionInfo relInfo)
        {
            string strSQL;

            List<MasReligionInfo> infoList = new List<MasReligionInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Sequence", relInfo.Sequence);

                strSQL = "SELECT RELIGION_SEQ, RELIGION_NAME, RELIGION_NAME_ENG, CREATED_BY, UPDATED_BY"
                    + " FROM MAS_RELIGIONS"
                    + " WHERE RECORD_STATUS not like @DeleteRecord"
                    + (relInfo.Sequence > 0 ? " AND RELIGION_SEQ = @Sequence" : "")
                    + " ORDER BY RELIGION_NAME";

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    MasReligionInfo Info = new MasReligionInfo(rdr.GetInt32(0)
                            , rdr.IsDBNull(1) ? "" : rdr.GetString(1)
                            , rdr.IsDBNull(2) ? "" : rdr.GetString(2)
                            , rdr.IsDBNull(3) ? (int?)null : rdr.GetInt32(3)
                            , rdr.IsDBNull(4) ? (int?)null : rdr.GetInt32(4));
                    infoList.Add(Info);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }

            return infoList;
        }
        
        public void Insert(MasReligionInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(5);

                dbManager.AddParameters(0, "@Name", Info.Name);
                dbManager.AddParameters(1, "@NameEng", Info.NameEng);
                dbManager.AddParameters(2, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(3, "@CreatedBy", Info.CreatedBy);
                dbManager.AddParameters(4, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO MAS_RELIGIONS"
                    + " (RELIGION_NAME, RELIGION_NAME_ENG, RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES (@Name, @NameEng, @RecordStatus, @CreatedBy, @CreatedWhen)";
                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(MasReligionInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(5);

                dbManager.AddParameters(0, "@Sequence", Info.Sequence);
                dbManager.AddParameters(1, "@Name", Info.Name);
                dbManager.AddParameters(2, "@NameEng", Info.NameEng);
                dbManager.AddParameters(3, "@UpdatedBy", Info.UpdatedBy);
                dbManager.AddParameters(4, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE MAS_RELIGIONS"
                    + " SET RELIGION_NAME = @Name"
                        + ", RELIGION_NAME_ENG = @NameEng"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE RELIGION_SEQ = @Sequence";
                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Delete(int Sequence, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@RecordStatus", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@UpdatedBy", UpdatedBy);
                dbManager.AddParameters(3, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE MAS_RELIGIONS"
                    + " SET RECORD_STATUS = @RecordStatus"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE RELIGION_SEQ = @Sequence";
                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
